<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>APK Static Report - {{ apk_filename }}</title>
	<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
	<script src="http://d3js.org/d3.v3.min.js"></script>
    <style type="text/css">
		body { font-family: Arial, sans-serif; }
		a { text-decoration: none; }

		.node circle {
			fill: #fff;
			stroke: steelblue;
			stroke-width: 2px;
		}

		.node text {
			font: 11px sans-serif;
			  text-shadow: 0 0 3px #fff;
		}

		.link {
			fill: none;
			stroke: #ccc;
		}

		.perm-dangerous {
			color: #ff3333;
		}
		thead td {
			font-weight: bold;
		}
		tbody td {
			border-top: 1px solid #ccc;
		}
    </style>
	<script>
	function toggle(e) {
		var e = e.parentNode;
		var exp = $(e).hasClass("expanded");

		if (exp) {
			$(e).removeClass('expanded');
			$(".arrow", e).html("&#x25ba;");
			$("pre", e).hide();
		} else {
			$(e).addClass('expanded');
			$(".arrow", e).html("&#x25bc;");
			$("pre", e).show();
		}
		return false;
	}

	var permission_callers_data = {name:'',children: {{- permission_callers|jsonify -}} };

	function draw_perm_vis(treeDataRoot) {
		// estimate height
		var est_height = 0;
		treeDataRoot.children.forEach(function(d) {
			est_height += d.children.length * 20; // pixels per method node
			est_height += 20;	// pixels per spacing between classes
		});

		var height = est_height,
			width = 550;
		var tree = d3.layout.tree().size([height, width]);
		var diagonal = d3.svg.diagonal().projection(function (d) { return [d.y, d.x]; });

		var svg = d3.select('#perm-vis svg')
			.attr('width', width)
			.attr('height', height);
		svg = svg.select('g');

		var nodes = tree.nodes(treeDataRoot),
			links = tree.links(nodes);

		// compute depth for each level with a multiplier
		nodes.forEach(function(d) { d.y = d.depth * [0, 0.5, 1][d.depth] * 180; });

		var i = 0; // assigned node id
		var n = svg.selectAll('g.node').data(nodes, function(d) { return d.id || (d.id = ++i); });

		var nodeEnter = n.enter().append('g').attr('class', 'node');

		nodeEnter.append('text')
			.attr('x', function(d) { return d.depth == 2 ? '1em' : '-1em'})
			.attr('y', function(d) { return d.depth == 2 ? '0.3em' : '1.8em'})
			.text(function(d) { return d.name; });

		nodeEnter.append('circle')
				.attr('r', 5);

		// setup node positions
		nodeEnter
			.attr('transform', function(d) { return 'translate(' + d.y + ',' + d.x + ')'; });

		// handle links
		var link = svg.selectAll('path.link')
			.data(links, function(d) { return d.target.id; });

		link.enter().insert('path', 'g')
			.attr('class', 'link')
			.attr('d', diagonal)
			.attr('stroke-width', function(d) {
				if (d.target.children)
					return d.target.children.length * 1.8;
				else if (d.target.num_children)
					return d.target.num_children;
				else
					return 2;
			});
	}
	window.onload = function() { draw_perm_vis(permission_callers_data); };

	</script>
  </head>

  <body>
	<h1>APK Static Analysis</h1>
	<h2>{{ apk_filename }}</h2>

	<h3>Activities</h3>
	{% for r in activities -%}
	<div>{{ r }} <code>{{ activities[r][0] }}</code></div>
	{%- endfor %}

	<h3>Services</h3>
	{% for svc in services -%}
	<div>{{ svc }}</div>
	{%- endfor %}

	<h3>Receivers</h3>
	{% for r in receivers -%}
	<div><a href="#" onclick="return toggle(this);"><span class="arrow">&#x25ba;</span>&nbsp;{{ r }} <code>{{ receivers[r][0] }}</code></a>
	  <pre class="prettyprint lang-java" style="display:none">
{%- for method_src in receivers[r][1] -%}
		{{- method_src + '\n\n' }}
{%- endfor -%}
	  </pre>
	</div>
	{%- endfor %}

	<h3>Permissions</h3>
	<p>This section lists the permissions requested by the APK, as well as a 
	visualization of which class &amp; method is invoking an API which requires permissions.</p>
	<div style="float:left; margin-right: 10em">
	<ul>
	{% for perm, desc in permissions.iteritems() -%}
		<li class="perm-{{ desc[0] }}" title="{{ desc[2] }}">{{ perm }}</li>
	{%- endfor %}
	</ul>
	</div>
	<div id="perm-vis" style="float:left">
		<svg><g transform="translate(-5,0)"/></svg>
	</div>
	<div style="clear:both"></div>

	<h3>Strings</h3>
	<p>This section contains strings referenced by the Dalvik code.</p>
	<table>
	<thead>
		<tr><td>usage count</td><td>string</td></tr>
	</thead>
	<tbody>
	{%- for s in code_strings -%}
		<tr><td>{{ code_strings[s][0] }}</td><td><code>{{ code_strings[s][1] | e }}</code></td></tr>
	{%- endfor -%}
	</tbody>
	</table>
  </body>
</html>
